/**
 * <p>Package : org.tea.springdata.mongdb</p>
 * <p>E-Mail : ztlyde@gmail.com</p>
 */
package org.tea.springdata.mongdb;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.tea.springdata.mongdb.dao.IAccountDao;
import org.tea.springdata.mongdb.dao.PersonRepository;
import org.tea.springdata.mongdb.domain.Account;
import org.tea.springdata.mongdb.domain.Person;

/**
 * Mongo单元测试
 * @author: John zhang
 * @version: 0.1
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, initializers = { AppInitializer.class }, loader = AnnotationConfigContextLoader.class)
public class MongoTest {
	
	@Autowired
	IAccountDao accDao;
	
	@Autowired
	PersonRepository personRepo;
	
	@Test
	public void register() {
		String username = "user3";
		String password = "pass3";
		accDao.register(new Account(username, password));
	}
	
	@Test
	public void login() {
		String username = "user3";
		String password = "pass3";
		accDao.login(new Account(username, password));
	}
	
	@Test
	public void addInfo() {
		Account acc = accDao.getTemplate().findById("51258eb9a75f33d4b129216a", Account.class);
		Person p = new Person("张三", 30);
		p.setAccount(acc);
		accDao.addInfo(p);
	}
	
	@Test
	public void findAll() {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(Account acc : accDao.findAll()) {
			System.out.println(String.format("账户名为:%s 最后登录时间为:%s", acc.getUsername(), fmt.format(new Date(acc.getLastLoginTime()))));
		}
	}
	
	@Test
	public void findByThePersonsAge() {
		List<Person> persons = personRepo.findByThePersonsAge(30);
		for(Person p : persons) {
			System.out.println(String.format("用户姓名为:%s 年龄为:%d", p.getName(), p.getAge()));
		}
	}
}
